set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/* *******************************************************************
** DATE :
** Writer :
** Memo :
******************************************************************** */
CREATE PROCEDURE [dbo].[TSP_Manager_GetList]
(
	@IsTransaction	tinyint			= 0
,	@ErrorCode		int				= 0		OUTPUT
,	@ErrorMessage	nvarchar( 1000 )= ''	OUTPUT

,	@strManagerID	varchar( 20 ) = null
,	@strManagerName	nvarchar( 50 ) = null

,	@PageNo	int	= 1	
,	@PageSize	tinyint	= 10	
,	@CntRow	int	= NULL	OUTPUT
,	@CntTotal	int	= NULL	OUTPUT
)
as
Set NOCOUNT ON

-- Frk Default Value
DECLARE @FrkDateNow DateTime
DECLARE @FrkRowCount int
DECLARE @FrkTranCount int
DECLARE @FrkErrorCode int
DECLARE @FrkErrorMessage nvarchar( 1000 )
Set @FrkDateNow = GetDate()
set @FrkTranCount = @@TRANCOUNT
-- Frk Check


If ( @IsTransaction = 0 )
Begin
	If ( @FrkTranCount < 1 )
	Begin
		Set @FrkErrorCode = -1
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Else If ( @FrkTranCount > 1 )
	Begin
		Set @FrkErrorCode = -2
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
End
Else
Begin
	If ( @FrkTranCount <> 0 )
	Begin
		Set @FrkErrorCode = -3
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Begin Tran
End

-- Implementation
DECLARE @SkipPage INT
SET @SkipPage = ( ( @PageNo - 1 ) * @PageSize )

IF ( @strManagerID Is null and @strManagerName is null )
begin
	Select
		@CntTotal = Count(1)
	From
		dbo.TB_Manager with ( nolock )
	IF ( @PageNo = 1 )
	BEGIN
		SELECT	Top( @PageSize )
			n4ManagerSN
		,	strManagerID
		,	strManagerPasshash
		,	strManagerName
		,	isManager
		FROM
			dbo.TB_Manager with ( nolock )
		ORDER BY
			n4ManagerSN DESC
	END
	ELSE
	BEGIN
		SELECT	Top( @PageSize )
			n4ManagerSN
		,	strManagerID
		,	strManagerPasshash
		,	strManagerName
		,	isManager
		FROM
		(
			SELECT
				n4ManagerSN
			,	strManagerID
			,	strManagerPasshash
			,	strManagerName
			,	isManager
			,	ROW_NUMBER() OVER ( ORDER BY
				n4ManagerSN DESC
			) AS ROWNUM
			FROM
				dbo.TB_Manager with ( nolock )
		)	A
		WHERE
			ROWNUM > @SkipPage
		AND	ROWNUM <= @SkipPage + @PageSize
		
	END
end
else IF ( @strManagerID Is not null and @strManagerName is null )
begin
	Select
		@CntTotal = Count(1)
	From
		dbo.TB_Manager with ( nolock )
	where
		strManagerID like @strManagerID + '%'
		
	IF ( @PageNo = 1 )
	BEGIN
		SELECT	Top( @PageSize )
			n4ManagerSN
		,	strManagerID
		,	strManagerPasshash
		,	strManagerName
		,	isManager
		FROM
			dbo.TB_Manager with ( nolock )
		where
			strManagerID like @strManagerID + '%'
		ORDER BY
			n4ManagerSN DESC
	END
	ELSE
	BEGIN
		SELECT	Top( @PageSize )
			n4ManagerSN
		,	strManagerID
		,	strManagerPasshash
		,	strManagerName
		,	isManager
		FROM
		(
			SELECT
				n4ManagerSN
			,	strManagerID
			,	strManagerPasshash
			,	strManagerName
			,	isManager
			,	ROW_NUMBER() OVER ( ORDER BY
				n4ManagerSN DESC
			) AS ROWNUM
			FROM
				dbo.TB_Manager with ( nolock )
			where
				strManagerID like @strManagerID + '%'
		)	A
		WHERE
			ROWNUM > @SkipPage
		AND	ROWNUM <= @SkipPage + @PageSize
		
	END
end
else IF ( @strManagerID Is null and @strManagerName is not null )
begin
	Select
		@CntTotal = Count(1)
	From
		dbo.TB_Manager with ( nolock )
	where
		strManagerName like @strManagerName + '%'
		
	IF ( @PageNo = 1 )
	BEGIN
		SELECT	Top( @PageSize )
			n4ManagerSN
		,	strManagerID
		,	strManagerPasshash
		,	strManagerName
		,	isManager
		FROM
			dbo.TB_Manager with ( nolock )
		where
			strManagerName like @strManagerName + '%'
		ORDER BY
			strManagerName DESC
	END
	ELSE
	BEGIN
		SELECT	Top( @PageSize )
			n4ManagerSN
		,	strManagerID
		,	strManagerPasshash
		,	strManagerName
		,	isManager
		FROM
		(
			SELECT
				n4ManagerSN
			,	strManagerID
			,	strManagerPasshash
			,	strManagerName
			,	isManager
			,	ROW_NUMBER() OVER ( ORDER BY
				strManagerName DESC
			) AS ROWNUM
			FROM
				dbo.TB_Manager with ( nolock )
			where
				strManagerName like @strManagerName + '%'
		)	A
		WHERE
			ROWNUM > @SkipPage
		AND	ROWNUM <= @SkipPage + @PageSize
		
	END
end


-- Frk Error Check
SELECT
	@FrkErrorCode = @@ERROR
,	@FrkRowCount = @@RowCount

set @CntRow = @FrkRowCount
-- End

COMPLETE:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Commit Tran
		End
	End
	SET @ErrorCode = 0
	SET @ErrorMessage = ''
	return 0
ERROR:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Rollback Tran
		End
	End
	SET @ErrorCode = @FrkErrorCode
	SET @ErrorMessage = @FrkErrorMessage
	return @FrkErrorCode
